package com.qbw.app.module.common.fragment;

import android.app.Activity;
import android.content.DialogInterface;
import android.os.Bundle;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;

import android.text.Html;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.View;

import com.qbw.app.R;
import com.qbw.app.databinding.CommonFragmentIKnowBinding;
import com.qbw.core.base.BaseDialogFragment;

/*
 *
 * @author QBW
 * @date 2019/8/23
 */
public class IKnowDialogFragment extends BaseDialogFragment<CommonFragmentIKnowBinding> {

    public static final String KEY_TITLE = "key_title";
    public static final String KEY_TITLE_GRAVITY = "key_title_gravity";
    public static final String KEY_TITLE_HTML_TEXT = "key_title_html_text";

    private int mType;
    private String mTitle;
    private int mTitleGravity;
    private String mTitleHtmlText;

    public static IKnowDialogFragment createInstance(String title) {
        return createInstance(title, Gravity.LEFT, "");
    }

    public static IKnowDialogFragment createInstance(int gravity, String titleHtmlText) {
        return createInstance("", gravity, titleHtmlText);
    }

    public static IKnowDialogFragment createInstance(String title,
                                                     int gravity,
                                                     String titleHtmlText) {
        IKnowDialogFragment ins = new IKnowDialogFragment();
        Bundle args = new Bundle();
        args.putString(KEY_TITLE, title);
        args.putInt(KEY_TITLE_GRAVITY, gravity);
        args.putString(KEY_TITLE_HTML_TEXT, titleHtmlText);
        ins.setArguments(args);
        return ins;
    }

    @Override
    protected int onGetLayoutId() {
        return R.layout.common_fragment_i_know;
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Bundle args = savedInstanceState == null ? getArguments() : savedInstanceState;
        mTitle = args.getString(KEY_TITLE);
        mTitleGravity = args.getInt(KEY_TITLE_GRAVITY);
        mTitleHtmlText = args.getString(KEY_TITLE_HTML_TEXT);
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        mBinding.tvTitle.setGravity(mTitleGravity);
        if (!TextUtils.isEmpty(mTitle)) {
            mBinding.tvTitle.setText(mTitle);
        } else if (!TextUtils.isEmpty(mTitleHtmlText)) {
            mBinding.tvTitle.setText(Html.fromHtml(mTitleHtmlText));
        }
        mBinding.tvIKnow.setOnClickListener(v -> {
            dismiss();
        });
    }

    @Override
    public void onDismiss(DialogInterface dialog) {
        super.onDismiss(dialog);
        Fragment pf = getParentFragment();
        if (pf instanceof Listener) {
            ((Listener) pf).onIKnowDialogDismiss(mType);
        } else {
            Activity activity = getActivity();
            if (activity instanceof Listener) {
                ((Listener) activity).onIKnowDialogDismiss(mType);
            }
        }
    }

    @Override
    public void onSaveInstanceState(@NonNull Bundle outState) {
        super.onSaveInstanceState(outState);
        outState.putString(KEY_TITLE, mTitle);
        outState.putInt(KEY_TITLE_GRAVITY, mTitleGravity);
        outState.putString(KEY_TITLE_HTML_TEXT, mTitleHtmlText);
    }

    public interface Listener {
        void onIKnowDialogDismiss(int type);
    }
}
